import 'package:flutter/material.dart';
import 'home_page.dart';
import 'category_page.dart';
import 'cart_page.dart';
import 'member_page.dart';
import 'package:provide/provide.dart';
import 'package:our_library/provides/index.dart';

class IndexPage extends StatefulWidget {
  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage>
    with SingleTickerProviderStateMixin {
  //数组存储底部导航栏按钮
  final List<BottomNavigationBarItem> bottomTabs = [
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      activeIcon: Chip(
          backgroundColor: Color.fromRGBO(68, 138, 255, 0.1),
          avatar: Icon(Icons.home),
          label: Text('首页', style: TextStyle(color: Colors.blueAccent))),
      title: Text(''),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.search),
      activeIcon: Chip(
        backgroundColor: Color.fromRGBO(68, 138, 255, 0.1),
        avatar: Icon(
          Icons.search,
          color: Colors.blueAccent,
        ),
        label: Text(
          '分类',
          style: TextStyle(color: Colors.blueAccent),
        ),
      ),
      title: Text(''),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.shopping_cart),
      activeIcon: Chip(
        backgroundColor: Color.fromRGBO(68, 138, 255, 0.1),
        avatar: Icon(
          Icons.shopping_cart,
          color: Colors.blueAccent,
        ),
        label: Text(
          '借阅车',
          style: TextStyle(color: Colors.blueAccent),
        ),
      ),
      title: Text(''),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.person),
      activeIcon: Chip(
        backgroundColor: Color.fromRGBO(68, 138, 255, 0.1),
        avatar: Icon(
          Icons.person,
          color: Colors.blueAccent,
        ),
        label: Text(
          '个人',
          style: TextStyle(color: Colors.blueAccent),
        ),
      ),
      title: Text(''),
    ),
  ];

  final List<Widget> tabBodies = [
    HomePage(),
    CategoryPage(),
    CartPage(),
    MemberPage()
  ];

  @override
  Widget build(BuildContext context) {
    return Provide<IndexPageProvide>(
      builder: (context, child, indexInfo) {
        return Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            elevation: 20,
            selectedFontSize: 0,
            unselectedFontSize: 0,
            currentIndex: indexInfo.getIndex(),
            items: bottomTabs,
            onTap: (index) {
              indexInfo.setIndex(index);
            },
          ),
          body: IndexedStack(
            children: tabBodies,
            index: indexInfo.getIndex(),
          ),
        );
      },
    );
  }
}
